
var all_version_infos = null;
var sayabc_root_url = "http://appmonitor.sayabc.com/api";

function totaluserinfo(json){
	var jsonObject = eval(json);
	var totalUserCount = jsonObject.totalUserCount;
	var yesterdayNewUserCount = jsonObject.yesterdayNewUserCount;
	var yesterdayUserCount = jsonObject.yesterdayUserCount;
	var yesterdayNewUserCountTemp = yesterdayNewUserCount == 0 ? 0 : Math.floor(parseFloat(yesterdayNewUserCount)/parseFloat(yesterdayUserCount) * 100) ;
	var yesterdayNewUserCountPercent = yesterdayNewUserCountTemp + '%'
	
	document.getElementById('yesterday_new_user_count').innerHTML= yesterdayNewUserCount;
	document.getElementById('yesterday_user_count').innerHTML= yesterdayUserCount;
	document.getElementById('total_user_count').innerHTML= totalUserCount;
	document.getElementById('yesterday_new_user_count_percent').innerHTML	=yesterdayNewUserCountPercent
}

function versionspread(json){
	
	var index = 1;
	var jsonObject = eval(json);
	
	for(var jsonNode in jsonObject)
	{
		var versionInfo = jsonObject[jsonNode].versionInfo;
		var versionCount = jsonObject[jsonNode].versionCount;
		var yesterdayVersionCount = jsonObject[jsonNode].yesterdayVersionCount;
		
		var rowTem1 = '<tr><td>' + index + '</td><td>' + versionInfo + '</td><td>' + yesterdayVersionCount + '</td><td class="hidden-480">' + 
		versionCount + '</td><td><a href="#" class="btn mini purple" data-toggle="modal" data-target="#exampleModal" onclick="edit_to_click(event)" id="' + versionInfo +'" name="1">Edit</a></td></tr>'
		
		$("#table_version_spread tbody:last").append(rowTem1);
		index++;
	}
	
	console.log(jsonObject);
}

function rolespread(json){
	
	var index = 1;
	var jsonObject = eval(json);
	
	for(var jsonNode in jsonObject)
	{
		var roleType = jsonObject[jsonNode].roleType == 1 ? '老师' : '学生';
		var roleCount = jsonObject[jsonNode].roleCount;
		var yesterdayRoleType = jsonObject[jsonNode].yesterdayRoleType;
		
		var rowTem1 = '<tr><td>' + index + '</td><td>' + roleType + '</td><td>' + yesterdayRoleType + '</td><td class="hidden-480">' + 
		roleCount + '</td><td><a href="#" class="btn mini purple" data-toggle="modal" data-target="#exampleModal" onclick="edit_to_click(event)" id="' + roleType +'" name="3"><i class="icon-edit"></i> Edit</a></td></tr>'
		$("#table_role_spread tbody:last").append(rowTem1);
		index++;
	}
	
	console.log(jsonObject);
}

function regionspread(json){
	
	var index = 1;
	var jsonObject = eval(json);
	
	for(var jsonNode in jsonObject)
	{
		var userCity = jsonObject[jsonNode].userCity;
		var userCityCount = jsonObject[jsonNode].userCityCount;
		var yeasterdayUserCityCount = jsonObject[jsonNode].yeasterdayUserCityCount;
		
		var rowTem1 = '<tr><td>' + index + '</td><td>' + userCity + '</td><td>' + yeasterdayUserCityCount + '</td><td class="hidden-480">' + 
		userCityCount + '</td><td><a href="#" class="btn mini purple" data-toggle="modal" data-target="#exampleModal" onclick="edit_to_click(event)" id="' + userCity +'" name="2"><i class="icon-edit"></i> Edit</a></td></tr>'
		$("#table_region_spread tbody:last").append(rowTem1);
		index++;
	}
	
	console.log(jsonObject);
}

function systemversionspread(json){
	
	var index = 1;
	var jsonObject = eval(json);
	
	for(var jsonNode in jsonObject)
	{
		var systemVersion = jsonObject[jsonNode].systemVersion;
		var systemVersionCount = jsonObject[jsonNode].systemVersionCount;
		var yesterdaySystemVersionCount = jsonObject[jsonNode].yesterdaySystemVersionCount;
		
		var rowTem1 = '<tr><td>' + index + '</td><td>' + systemVersion + '</td><td>' + yesterdaySystemVersionCount + '</td><td class="hidden-480">' + 
		systemVersionCount + '</td><td><a href="#" class="btn mini purple" data-toggle="modal" data-target="#exampleModal" onclick="edit_to_click(event)" id="' + systemVersion +'" name="4"><i class="icon-edit"></i> Edit</a></td></tr>'
		$("#table_system_version_spread tbody:last").append(rowTem1);
		index++;
	}
	
	console.log(jsonObject);
}

function getuserinfolist(json){
	
	var jsonObject = eval(json);
	
	var userInfoList = jsonObject.userInfoList;
	var pageModel = jsonObject.pageModel;
	
	var index = 1;
	for(var jsonNode in userInfoList)
	{
		var userId = userInfoList[jsonNode].userId;
		var versionInfo = userInfoList[jsonNode].versionInfo;
		var userCity = userInfoList[jsonNode].userCity;
		var roleType = userInfoList[jsonNode].roleType == 1 ? '老师' : '学生';
		var systemVersion = userInfoList[jsonNode].systemVersion;
		
		var rowTem1 = '<tr>' + 
		'<td>' + userId +'</td>' +
		'<td>' + versionInfo +'</td>' + 
		'<td class="hidden-480">' + systemVersion +'</td>' + 
		'<td class="hidden-480">' + roleType +'</td>' + 
		'<td class="hidden-480">' + userCity +'</td>' + 
		'<td><a href="#" class="btn mini purple" data-toggle="modal" data-target="#exampleModal" onclick="edit_to_click(event)" id="' + userId +'" name="5"><i class="icon-edit"></i> Edit</a></td>' +
		'</tr>'
		
		$("#user_info_list tbody:last").append(rowTem1);
		index++;
	}
	
	console.log(jsonObject);
}

function getuserinfobyid(json){

	var jsonObject = eval(json);
	
	$("#user_info_list tr:not(:first)").empty("");
	
	var userId = jsonObject.userId;
	var versionInfo = jsonObject.versionInfo;
	var userCity = jsonObject.userCity;
	var roleType = jsonObject.roleType == 1 ? '老师' : '学生';
	var systemVersion = jsonObject.systemVersion;

	var rowTem1 = '<tr>' + 
	'<td>' + userId +'</td>' +
	'<td>' + versionInfo +'</td>' + 
	'<td class="hidden-480">' + systemVersion +'</td>' + 
	'<td class="hidden-480">' + roleType +'</td>' + 
	'<td class="hidden-480">' + userCity +'</td>' + 
	'<td><a href="#" class="btn mini purple" data-toggle="modal" data-target="#exampleModal" onclick="edit_to_click(event)" id="' + userId +'" name="5"><i class="icon-edit"></i> Edit</a></td>' +
	'</tr>'
	
	$("#user_info_list tbody:last").append(rowTem1);
	
	console.log(jsonObject);
}

function getallversioninfo(json){
	var jsonObject = eval(json);
	
	all_version_infos = jsonObject;
	$("#version_info_list tr:not(:first)").empty("");

	for(var jsonNode in jsonObject)
	{
		var versionId = jsonObject[jsonNode].versionId;
		var versionInfo = jsonObject[jsonNode].versionInfo;
		var roleType = jsonObject[jsonNode].roleType == 1 ? '老师' : '学生';
		var isNewest = jsonObject[jsonNode].isNewest;
		var filesInfo = jsonObject[jsonNode].filesInfo;

		var rowTem1 = '<tr>' + 
		'<td>' + versionInfo +'</td>' + 
		'<td class="hidden-480">' + roleType +'</td>' + 
		'<td class="hidden-480">' + isNewest +'</td>' + 
		'<td><a href="#" class="btn mini purple" onclick="version_edit_click(event)" id="' + versionId +'" name="' + filesInfo + '"><i class="icon-edit"></i> Edit</a></td>' +
		'</tr>'
		
		$("#version_info_list tbody:last").append(rowTem1);
	}
	
	console.log(jsonObject);
}


function version_edit_click(event){	
	
	var info_id = event.srcElement.id;
	
	for(var jsonNode in all_version_infos)
	{
		var versionId = all_version_infos[jsonNode].versionId;
		if (info_id == versionId)
		{
			var versionInfo = all_version_infos[jsonNode].versionInfo;
			var roleType = all_version_infos[jsonNode].roleType == 1 ? '老师' : '学生';
			var isNewest = all_version_infos[jsonNode].isNewest;
			var filesInfo = all_version_infos[jsonNode].filesInfo;

			$("#client_version_info").val(versionInfo); 
			$("#version_files_info").val(filesInfo); 
			$("#is_newest_verison").val(isNewest);
			$("#role_type_info").val(roleType);
			
			break;
		}
	}
	
	console.log("11111111111111111111");
}

function submit_info_server(event){	
	
	var versionInfo = $("#client_version_info").val(); 
	var filesInfo = $("#version_files_info").val(); 
	var isNewest = $("#is_newest_verison").val();
	var roleType = $("#role_type_info").val() == '老师' ? '1' : '2';
	
	var postData = JSON.stringify({"newestVersionInfo" : versionInfo, "newestVersionFileInfo" : filesInfo, "roleType" : roleType, "isNewest" : isNewest});
	
	$.getScript(sayabc_root_url + '/ui/addversion?versioninfo=' + versionInfo + '&fileinfo=' + filesInfo + '&roleType=' + roleType + '&isNewest=' + isNewest);
	
	
	console.log(postData);
	
	//location.reload();
}

var SayABCHomePage = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/totaluserinfo?callback=totaluserinfo');
        }
	};
}();

var SayABCDataPageVersionSpread = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/versionspread?callback=versionspread');
        }
	};
}();

var SayABCDataPageRoleSpread = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/rolespread?callback=rolespread');
        }
	};
}();

var SayABCDataPageRegionSpread = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/regionspread?callback=regionspread');
        }
	};
}();

var SayABCDataPageSystemVersionSpread = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/systemversionspread?callback=systemversionspread');
        }
	};
}();

var SayABCUserPageInfo = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/getuserinfolist?callback=getuserinfolist&page=0');
        }
	};
}();

var SayABCUserPageInfoById = function () {
	
	return {
        init: function (userId) {
			$.getScript(sayabc_root_url + '/ui/getuserinfobyid?callback=getuserinfobyid&userid=' + userId);
        }
	};
}();

var SayABCVersionPage = function () {
	
	return {
        init: function () {
			$.getScript(sayabc_root_url + '/ui/getallversioninfo?callback=getallversioninfo');
        }
	};
}();



























